// pages/details/details.js
var call = require('../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      details:{
        id: '',//标识
        pic: '',
        name:'',
        tag:'',
        peoplenum:'',
        preparetime:'',
        cookingtime:'',
        material: [],//用料
        process: [],//做法
        isAdd:true,
        nodes:[]
      },
  },
  //方法
  pagedetails: function () {
    var that = this;
    //参数
    let url = '/recipe/search'
    let date = {
      keyword: that.data.name,
      num: 1,
      appkey: '928cbbc1f0636217'
    }
    call.request(url, date).then(res => {
      // console.log('res', res)
      that.setData({ details: res.data.result.list[0] })//修改
    })
  },

  //添加收藏
  addFavorites:function(){
    let that = this;
    that.setData({ isAdd:true})
    // console.log(that.data.details,'数据')
    let details = that.data.details
    let id = String(details.id)
    wx.setStorageSync(id, details); //添加到本地缓存
  },
  //取消收藏
  cancelFavorites:function(){
    let that = this;
    that.setData({ isAdd: false })
    let details = that.data.details
    let id = String(details.id)
    wx.removeStorageSync(id);//从本地缓存删除
    console.log(that.data.isAdd, '取消收藏')
  },
  //分享
  partake_btn:function(){
    let that = this;
    // console.log('分享',that.data.name)
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // console.log('接受参数')
    let date = JSON.parse(options.date)
    let id = String(date.id)
    that.setData({ name: date.name})
    that.pagedetails()//调用接口

    //检查当前新闻是否在收藏夹中
    var article = wx.getStorageSync(id);
    //已存在
    if (article != '') {
      this.setData({ isAdd: true })
    }
    //不存在
    else {
      this.setData({ isAdd: false })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    console.log('打印date的数据',that.data.name)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})